﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using static Apewer.Source.SourceUtility;

#if NETFRAMEWORK
using System.Data.Sql;
#endif

namespace Apewer.Source
{

    /// <summary>连接 SQL Server 数据库的客户端。</summary>
    [Serializable]
    public class SqlClient : DbClient
    {

        #region connection

        private SqlConnection _conn = null;
        private string _connstr = "";

        /// <summary>使用连接字符串创建数据库连接实例。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public SqlClient(IDbConnection connection, Timeout timeout = null) : base(timeout)
        {
            if (connection == null) throw new ArgumentNullException(nameof(connection), "指定的连接无效。");

            var conn = connection as SqlConnection;
            if (conn == null) throw new ArgumentException(nameof(connection), "指定的连接不是支持的类型。");

            _conn = conn;
            _connstr = conn.ConnectionString;
        }

        /// <summary>使用连接字符串创建数据库连接实例。</summary>
        public SqlClient(string connectionString, Timeout timeout = null) : base(timeout)
        {
            _connstr = connectionString ?? "";
        }

        /// <summary>使用连接凭据创建数据库连接实例。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        public SqlClient(string address, string store, string user, string pass, Timeout timeout = null) : base(timeout)
        {
            if (address.IsEmpty()) throw new ArgumentNullException(nameof(address));
            if (store.IsEmpty()) store = "master";

            var cs = $"data source = {address ?? ""}; initial catalog = {store}; ";
            if (string.IsNullOrEmpty(user)) cs += "integrated security = sspi; ";
            else
            {
                cs += $"user id = {user}; ";
                if (!string.IsNullOrEmpty(pass)) cs += $"password = {pass}; ";
            }
            if (timeout != null) cs += $"connection timeout = {timeout.Connect}; ";

            _connstr = cs;
        }

        #endregion

        #region override

        /// <summary>查询数据库中的所有表名。</summary>
        public override string[] TableNames() => QueryStrings("select [name] from [sysobjects] where [type] = 'u' order by [name]");

        /// <summary>查询数据库实例中的所有数据库名。</summary>
        public override string[] StoreNames() => QueryStrings("select [name] from [master]..[sysdatabases] order by [name]", new string[] { "master", "model", "msdb", "tempdb" });

        /// <summary>查询表中的所有列名。</summary>
        public override string[] ColumnNames(string tableName) => QueryStrings($"select [name] from [syscolumns] where [id] = object_id('{TextUtility.AntiInject(tableName)}')");

        /// <summary>创建表，当表不存在时创建表，当现存表中缺少模型中属性对应的列时增加列。成功时返回空字符串，发生异常时返回异常信息。</summary>
        protected override string Initialize(TableStructure structure, string table)
        {
            var model = structure.Model;
            if (string.IsNullOrEmpty(table)) table = structure.TableName;

            // 检查现存表。
            var exists = false;
            var tables = TableNames();
            if (tables.Length > 0)
            {
                var lower = table.ToLower();
                foreach (var tn in tables)
                {
                    if (TextUtility.IsEmpty(tn)) continue;
                    if (tn.ToLower() == lower)
                    {
                        exists = true;
                        break;
                    }
                }
            }

            if (exists)
            {
                // 获取已存在的列名。
                var columns = ColumnNames(table);
                if (columns.Length > 0)
                {
                    var lower = new List<string>();
                    foreach (var column in columns)
                    {
                        if (TextUtility.IsBlank(column)) continue;
                        lower.Add(column.ToLower());
                    }
                    columns = lower.ToArray();
                }

                // 增加列。
                foreach (var column in structure.Columns)
                {
                    // 检查 Independent 特性。
                    if (structure.Independent && column.Independent) continue;

                    // 去重。
                    var lower = column.Field.ToLower();
                    if (columns.Contains(lower)) continue;

                    var type = Declaration(column);
                    if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");

                    var sql = TextUtility.Merge("alter table [", table, "] add ", type, "; ");
                    var execute = Execute(sql, null, false);
                    if (execute.Success == false) return execute.Message;
                }
                return TextUtility.Empty;
            }
            else
            {
                var sqlcolumns = new List<string>();
                foreach (var column in structure.Columns)
                {
                    // 检查 Independent 特性。
                    if (structure.Independent && column.Independent) continue;

                    var type = Declaration(column);
                    if (!column.Independent)
                    {
                        if (column.PrimaryKey) type = type + " primary key";
                        if (column.Incremental) type += " identity";
                    }

                    if (string.IsNullOrEmpty(type)) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
                    sqlcolumns.Add(type);
                }
                if (sqlcolumns.Count < 1) return $"无法对类型 {model.FullName} 创建表：没有定义任何字段。";
                var sql = TextUtility.Merge("create table [", table, "](", string.Join(", ", sqlcolumns.ToArray()), "); ");
                var execute = Execute(sql, null, false);
                if (execute.Success) return TextUtility.Empty;
                return execute.Message;
            }
        }

        /// <summary>插入记录。返回错误信息。</summary>
        public override string Insert(object record, string table = null, bool adjust = true)
        {
            if (record == null) return "参数无效。";
            if (adjust) FixProperties(record);

            var structure = TableStructure.Parse(record.GetType());
            if (structure == null) return "无法解析记录模型。";
            if (string.IsNullOrEmpty(table)) table = structure.TableName;
            if (string.IsNullOrEmpty(table)) return "表名称无效。";

            // 排除字段。
            var excluded = new List<string>();
            foreach (var ca in structure.Columns)
            {
                // 排除不使用 ORM 的属性。
                if (ca.Independent || ca.Incremental) excluded.Add(ca.Field);
            }

            var ps = structure.CreateParameters(record, Parameter, excluded);
            var psc = ps.Length;
            if (psc < 1) return "数据模型不包含字段。";

            var names = new List<string>(psc);
            var values = new List<string>(psc);
            foreach (var column in ps)
            {
                //names.Add(TextGenerator.Merge("[", column, "]"));
                names.Add(TextUtility.Merge(column));
                values.Add("@" + column);
            }
            var sb = new StringBuilder();
            sb.Append("insert into ", table, "(", string.Join(", ", names.ToArray()), ") ");
            sb.Append("values(", string.Join(", ", values.ToArray()), "); ");
            var sql = sb.ToString();

            var execute = Execute(sql, ps, false);
            if (execute.Success) return TextUtility.Empty;
            return execute.Message;
        }

        /// <summary>更新记录，实体中的 Key 属性不被更新。返回错误信息。</summary>
        /// <remarks>不更新带有 Independent 特性的模型（缺少 Key 属性）。</remarks>
        public override string Update(IRecord record, string table = null, bool adjust = true)
        {
            if (record == null) return "参数无效。";
            if (adjust)
            {
                FixProperties(record);
                SetUpdated(record);
            }

            var structure = TableStructure.Parse(record.GetType());
            if (structure == null) return "无法解析记录模型。";
            if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
            if (string.IsNullOrEmpty(table)) table = structure.TableName;
            if (string.IsNullOrEmpty(table)) return "表名称无效。";

            // 排除字段。
            var excluded = new List<string>();
            if (structure.Key != null) excluded.Add(structure.Key.Field);
            foreach (var ca in structure.Columns)
            {
                // 排除不使用 ORM 的属性、自增属性和主键属性。
                if (ca.Independent || ca.Incremental || ca.PrimaryKey || ca.NoUpdate) excluded.Add(ca.Field);
            }

            var ps = structure.CreateParameters(record, Parameter, excluded);
            var psc = ps.Length;
            if (psc < 1) return "数据模型不包含字段。";

            var items = new List<string>();
            foreach (var p in ps)
            {
                var pn = p.ParameterName;
                items.Add(TextUtility.Merge("[", pn, "] = @", pn));
            }
            var key = record.Key.SafeKey();
            var sql = $"update {table} set {string.Join(", ", items.ToArray())} where [{structure.Key.Field}]='{key}'";

            var execute = Execute(sql, ps, false);
            if (execute.Success) return TextUtility.Empty;
            return execute.Message;
        }

        /// <summary></summary>
        public override string ConnectionString => _connstr;

        /// <summary></summary>
        protected override IDataAdapter CreateDataAdapter(IDbCommand command) => new SqlDataAdapter((SqlCommand)command);

        /// <summary></summary>
        protected override IDbConnection GetConnection()
        {
            if (_conn == null) _conn = new SqlConnection(_connstr);
            return _conn;
        }

        /// <summary></summary>
        protected override IDataParameter CreateParameter() => new SqlParameter();

        /// <summary></summary>
        protected override string Keys(string tableName, string keyField, string flagField, long flagValue)
        {
            if (flagValue == 0) return $"select [{keyField}] from [{tableName}]";
            else return $"select [{keyField}] from [{tableName}] where [{flagField}] = {flagValue}";
        }

        /// <summary></summary>
        protected override string Get(string tableName, string keyField, string keyValue, string flagField, long flagValue)
        {
            if (flagValue == 0) return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}'";
            else return $"select top 1 * from [{tableName}] where [{keyField}] = '{keyValue}' and [{flagField}] = {flagValue}";
        }

        /// <summary></summary>
        protected override string List(string tableName, string flagField, long flagValue)
        {
            if (flagValue == 0) return $"select * from [{tableName}]";
            else return $"select * from [{tableName}] where [{flagField}] = {flagValue}";
        }

        #endregion

        #region special

        /// <summary>清空与指定连接关联的连接池。</summary>
        public static void ClearPool(IDbConnection connection)
        {
            var special = connection as SqlConnection;
            if (special != null)
            {
                try
                {
                    SqlConnection.ClearPool(special);
                }
                catch { }
            }
        }

        /// <summary>清空与指定连接关联的连接池。</summary>
        public static void ClearPool(IDbAdo instance)
        {
            var connection = instance?.Connection;
            if (connection != null) ClearPool(connection);
        }

        /// <summary>清空连接池。</summary>
        public static void ClearAllPools()
        {
            try
            {
                SqlConnection.ClearAllPools();
            }
            catch { }
        }

        /// <summary>获取列信息。</summary>
        public ColumnInfo[] ColumnsInfo(string tableName)
        {
            if (tableName.IsEmpty()) throw new ArgumentNullException(nameof(tableName));
            var sql = $"select name, xtype, length from syscolumns where id = object_id('{tableName}') ";
            using (var query = Query(sql))
            {
                var ab = new ArrayBuilder<ColumnInfo>();
                for (var i = 0; i < query.Rows; i++)
                {
                    var info = new ColumnInfo();
                    info.Name = query.Text(i, "name");
                    info.Type = XType(query.Int32(i, "xtype"));
                    info.Length = query.Int32(i, "length");
                    ab.Add(info);
                }
                return ab.Export();
            }
        }

        /// <summary>批量插入，必须在 DataTable 中指定表名，或指定 tableName 参数。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="Exception"></exception>
        public void BulkCopy(DataTable table, string tableName = null)
        {
            // 检查 table 参数。
            if (table == null) throw new ArgumentNullException(nameof(table));
            if (table.Rows.Count < 1) return;

            // 检查 tableName 参数。
            if (tableName.IsEmpty()) tableName = table.TableName;
            if (tableName.IsEmpty()) throw new ArgumentException("未指定表名。");

            // 连接数据库。
            var connect = Connect();
            if (connect.NotEmpty()) throw new Exception(connect);

            // 准备参数。
            var options = SqlBulkCopyOptions.Default;
            var trans = Transaction as SqlTransaction;
            if (trans == null) options |= SqlBulkCopyOptions.UseInternalTransaction;

            // 批量插入。
            var bc = null as SqlBulkCopy;
            try
            {
                bc = new SqlBulkCopy((SqlConnection)Connection, options, trans);
                bc.DestinationTableName = tableName;
                bc.BatchSize = table.Rows.Count;
                bc.WriteToServer(table);
                try { bc.Close(); } catch { }
            }
            catch (Exception ex)
            {
                try { bc.Close(); } catch { }
                throw ex;
            }
        }

        /// <summary>创建数据库，返回错误信息。</summary>
        /// <param name="name">数据库名称。</param>
        /// <param name="logMaxSizeMB">日志文件的最大 MB 数，指定非正数将不限制增长。</param>
        /// <returns>成功时候返回 NULL 值，失败时返回错误信息。</returns>
        public string CreateStore(string name, int logMaxSizeMB = 1024)
        {
            var store = name.Escape().ToTrim();
            if (store.IsEmpty()) return "创建失败：未指定数据库名称。";
            if (ConnectionString.IsEmpty()) return "创建失败：未指定数据库连接方式。";

            using (var source = new SqlClient(ConnectionString))
            {
                var connect = source.Connect();
                if (connect.NotEmpty()) return "创建失败：" + connect;

                var schema = source.Cell("select default_schema_name from sys.database_principals where type = 'S' and name=user_name()");
                if (schema.IsEmpty()) return "创建失败：无法获取默认模式名称。";

                var refPath = source.Cell(@"select f.physical_name path from sys.filegroups g left join sys.database_files f on f.data_space_id = g.data_space_id where g.name = 'PRIMARY' and g.type = 'FG' and g.is_default = 1 and g.filegroup_guid is null");
                if (refPath.IsEmpty()) return "创建失败：无法获取文件路径。";

                var win = refPath.Substring(1, 2) == ":\\";
                var nix = refPath.StartsWith("/");
                if (!win && !nix) return "创建失败：暂不支持该服务器。";

                var mdfPath = store + ".mdf";
                var ldfPath = store + ".ldf";
                if (win)
                {
                    var refSplit = refPath.Split('\\');
                    var dir = string.Join("\\", refSplit, 0, refSplit.Length - 1);
                    mdfPath = dir + "\\" + mdfPath;
                    ldfPath = dir + "\\" + ldfPath;
                }
                if (nix)
                {
                    var refSplit = refPath.Split('/');
                    var dir = string.Join("/", refSplit, 0, refSplit.Length - 1);
                    mdfPath = dir + "/" + mdfPath;
                    ldfPath = dir + "/" + ldfPath;
                }

                // 创建库。
                var maxLog = logMaxSizeMB > 0 ? $"{logMaxSizeMB}MB" : "UNLIMITED";
                var sql1 = $@"
CREATE DATABASE [{store}]
ON PRIMARY
(
    NAME = N'{store}',
    FILENAME = N'{mdfPath}',
    SIZE = 4MB,
    MAXSIZE = UNLIMITED,
    FILEGROWTH = 4MB
)
LOG ON
(
    NAME = N'{store}_log',
    FILENAME = N'{ldfPath}',
    SIZE = 1MB,
    MAXSIZE = {maxLog},
    FILEGROWTH = 1MB
)
COLLATE Chinese_PRC_CI_AS
";
                var create = source.Execute(sql1, null, false);
                if (!create.Success) return TextUtility.Merge("创建失败：", create.Message);

                // 设置兼容性级别。
                var sql2 = $"alter database [{store}] set compatibility_level = 100";
                source.Execute(sql2, null, false);

                // 设置恢复默认为“简单”
                var sql3 = $"alter database [{store}] set recovery simple";
                source.Execute(sql3, null, false);

                return null;
            }
        }

        static string XType(int xtype)
        {
            switch (xtype)
            {
                case 34: return "image";
                case 35: return "text";
                case 36: return "uniqueidentifier";
                case 48: return "tinyint";
                case 52: return "smallint";
                case 56: return "int";
                case 58: return "smalldatetime";
                case 59: return "real";
                case 60: return "money";
                case 61: return "datetime";
                case 62: return "float";
                case 98: return "sql_variant";
                case 99: return "ntext";
                case 104: return "bit";
                case 106: return "decimal";
                case 108: return "numeric";
                case 122: return "smallmoney";
                case 127: return "bigint";
                case 165: return "varbinary";
                case 167: return "varchar";
                case 173: return "binary";
                case 175: return "char";
                case 189: return "timestamp";
                case 231: return "nvarchar";
                case 239: return "nchar";
                case 241: return "xml";
            }
            return null;
        }

#if NET20 || NET40

        /// <summary>枚举本地网络中的 SQL Server 实例的信息。</summary>
        public static Source[] EnumerateSources()
        {
            var list = new List<Source>();

            // 表中列名：ServerName、InstanceName、IsClustered、Version。
            using (var table = SqlDataSourceEnumerator.Instance.GetDataSources())
            {
                var query = new Query(table);
                var rows = query.Rows;
                list.Capacity = rows;
                for (int i = 0; i < rows; i++)
                {
                    var item = new Source();
                    item.ServerName = query.Text(i, "ServerName");
                    list.Add(item);
                }
            }
            return list.ToArray();
        }

        /// <summary>SQL Server 实例的信息。</summary>
        public sealed class Source
        {

            /// <summary></summary>
            public string ServerName { get; set; }

            /// <summary></summary>
            public string InstanceName { get; set; }

            /// <summary></summary>
            public string IsClustered { get; set; }

            /// <summary></summary>
            public string Version { get; set; }

        }

#endif

        /// <summary>创建参数。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        static SqlParameter Parameter(Parameter parameter)
        {
            if (parameter == null) throw new InvalidOperationException("参数无效。");
            return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
        }

        /// <summary>创建参数。</summary>
        public static SqlParameter Parameter(string name, ColumnType type, int size, object value)
        {
            var vname = TextUtility.Trim(name);
            if (TextUtility.IsBlank(vname)) return null;

            var vtype = SqlDbType.BigInt;
            switch (type)
            {
                case ColumnType.Boolean:
                    vtype = SqlDbType.Bit;
                    break;
                case ColumnType.Bytes:
                    vtype = SqlDbType.Image;
                    break;
                case ColumnType.Integer:
                    vtype = SqlDbType.BigInt;
                    break;
                case ColumnType.Float:
                    vtype = SqlDbType.Float;
                    break;
                case ColumnType.DateTime:
                    vtype = SqlDbType.DateTime;
                    break;
                case ColumnType.VarChar:
                case ColumnType.VarChar191:
                case ColumnType.VarCharMax:
                    vtype = SqlDbType.VarChar;
                    break;
                case ColumnType.NVarChar:
                case ColumnType.NVarChar191:
                case ColumnType.NVarCharMax:
                    vtype = SqlDbType.NVarChar;
                    break;
                case ColumnType.Text:
                    vtype = SqlDbType.Text;
                    break;
                case ColumnType.NText:
                    vtype = SqlDbType.NText;
                    break;
                default:
                    throw new InvalidOperationException(TextUtility.Merge("类型 ", type.ToString(), " 不受支持。"));
            }

            var vsize = size;
            switch (type)
            {
                case ColumnType.VarChar:
                    vsize = NumberUtility.Restrict(vsize, 0, 8000);
                    break;
                case ColumnType.NVarChar:
                    vsize = NumberUtility.Restrict(vsize, 0, 4000);
                    break;
                case ColumnType.VarChar191:
                case ColumnType.NVarChar191:
                    vsize = NumberUtility.Restrict(vsize, 0, 191);
                    break;
                default:
                    vsize = 0;
                    break;
            }

            var vvalue = value ?? DBNull.Value;
            if (vvalue is string && vvalue != null && vsize > 0)
            {
                vvalue = TextUtility.Left((string)vvalue, vsize);
            }

            var parameter = new SqlParameter();
            parameter.ParameterName = vname;
            parameter.SqlDbType = vtype;
            parameter.Value = vvalue;
            if (vsize > 0) parameter.Size = vsize;
            return parameter;
        }

        /// <summary>创建参数。</summary>
        public static SqlParameter Parameter(string name, SqlDbType type, int size, object value)
        {
            if (value is string && value != null && size > 0)
            {
                value = TextUtility.Left((string)value, (int)size);
            }

            var p = new SqlParameter();
            p.ParameterName = name ?? "";
            p.SqlDbType = type;
            p.Size = size;
            p.Value = value ?? DBNull.Value;
            return p;
        }

        /// <summary>创建参数。</summary>
        public static SqlParameter Parameter(string name, SqlDbType type, object value)
        {
            var p = new SqlParameter();
            p.ParameterName = name ?? "";
            p.SqlDbType = type;
            p.Value = value ?? DBNull.Value;
            return p;
        }

        static string Declaration(ColumnAttribute column)
        {
            var type = TextUtility.Empty;
            var vcolumn = column;
            var length = Math.Max(0, vcolumn.Length);
            switch (vcolumn.Type)
            {
                case ColumnType.Boolean:
                    type = "bit";
                    break;
                case ColumnType.Integer:
                    type = "bigint";
                    break;
                case ColumnType.Float:
                    type = "float";
                    break;
                case ColumnType.Bytes:
                    type = "image";
                    break;
                case ColumnType.DateTime:
                    type = "datetime";
                    break;
                case ColumnType.VarChar:
                    type = TextUtility.Merge("varchar(", Math.Min(8000, length).ToString(), ")");
                    break;
                case ColumnType.VarChar191:
                    type = TextUtility.Merge("varchar(191)");
                    break;
                case ColumnType.VarCharMax:
                    type = TextUtility.Merge("varchar(max)");
                    break;
                case ColumnType.Text:
                    type = TextUtility.Merge("text");
                    break;
                case ColumnType.NVarChar:
                    type = TextUtility.Merge("nvarchar(", Math.Min(4000, length).ToString(), ")");
                    break;
                case ColumnType.NVarChar191:
                    type = TextUtility.Merge("nvarchar(191)");
                    break;
                case ColumnType.NVarCharMax:
                    type = TextUtility.Merge("nvarchar(max)");
                    break;
                case ColumnType.NText:
                    type = TextUtility.Merge("ntext");
                    break;
                default:
                    return TextUtility.Empty;
            }
            return TextUtility.Merge("[", vcolumn.Field, "] ", type);
        }

        #endregion

    }

}
